package com.example.leo.mycharmingstudent.a0829practice.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;

/**
 * Created by Leo on 2020/8/29.
 */

public class ClazzDemoFragment extends Fragment
        implements View.OnClickListener {

    private static final String TAG = "ClazzDemoFragment";

    private static final String FRAGMENT_NAME = "fragment_name";
    private static final String EMPTY_CONTENT = "啥？啥？啥？啥都没有";
    private String mFragmentName;

    public static ClazzDemoFragment createClazzDemoFragment(
            String fragmentName) {
        ClazzDemoFragment clazzDemoFragment = new ClazzDemoFragment();
        Bundle bundle = new Bundle();
        bundle.putString(FRAGMENT_NAME, fragmentName);
        clazzDemoFragment.setArguments(bundle);
        return clazzDemoFragment;
    }

    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mFragmentName = arguments.getString(FRAGMENT_NAME);
        }
        View inflate = inflater.inflate(R.layout.clazz_practice_fragment, null, false);
        TextView tvContent = inflate.findViewById(R.id.tv_content_clazz_practice_fragment);
        tvContent.setText(TextUtils.isEmpty(mFragmentName)
                ? EMPTY_CONTENT : mFragmentName);
        tvContent.setOnClickListener(this);

        return inflate;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //Fragment中内容按钮点击事件，设置标题
            case R.id.tv_content_clazz_practice_fragment:
                if (mOnClazzDemoListener != null) {
                    mOnClazzDemoListener
                            .modifyTitle(TextUtils.isEmpty(mFragmentName)
                                    ? EMPTY_CONTENT : mFragmentName);
                } else {
                    Log.e(TAG, "mOnClazzDemoListener == null!please check mOnClazzDemoListener.if you don't call setOnClazzDemoListener()");
                }
                break;
            default:
                break;
        }
    }

    public interface OnClazzDemoListener {
        /**
         * 修改标题
         *
         * @param text 标题文本内容
         */
        void modifyTitle(String text);
    }

    private OnClazzDemoListener mOnClazzDemoListener;

    public void setOnClazzDemoListener(
            OnClazzDemoListener onClazzDemoListener) {
        this.mOnClazzDemoListener = onClazzDemoListener;
    }
}
